import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {NzTreeVo} from "../../../../vo/NzTreeVo";
import {AlertService} from "../../../../services/alert.service";
import {NzModalService} from "ng-zorro-antd/modal";
import {NzContextMenuService, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {ManageService} from "../../../../services/manage.service";
import {NzFormatEmitEvent} from "ng-zorro-antd/core/tree";
import {ZyzceditComponent} from "../zyzcedit/zyzcedit.component";

@Component({
  selector: 'app-zyzclist',
  templateUrl: './zyzclist.component.html',
  styleUrls: ['./zyzclist.component.css']
})
export class ZyzclistComponent implements OnInit {
  cd: Array<NzTreeVo>
  gn: Array<NzTreeVo>
  activatedNode: any;

  constructor(private manageService: ManageService,
              private alertService: AlertService,
              private modal: NzModalService,
              private nzContextMenuService: NzContextMenuService,
              private viewContainerRef: ViewContainerRef) {
    this.cd = new Array<NzTreeVo>()
    this.gn = new Array<NzTreeVo>()
  }

  ngOnInit(): void {
    this.getcdArrayNztree()
    this.getgnArrayNztree()
  }

  onClickNode(data: NzFormatEmitEvent) {
    this.activatedNode = data.node!;
  }

  contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent, key: string): void {


    this.nzContextMenuService.create($event, menu);

  }
  getcdArrayNztree() {
    this.manageService.getRightNzTreeByTypeForZyzc("MENU").then((data: any) => {
      if (data.msg == "ok") {
        this.cd = data.list
      } else {
        this.alertService.showError("根据类型生产菜单树失败")
      }
    })
  }

  getgnArrayNztree() {
    this.manageService.getRightNzTreeByTypeForZyzc("ACTION").then((data: any) => {
      if (data.msg == "ok") {
        this.gn = data.list
      } else {
        this.alertService.showError("根据类型生产菜单树失败")
      }
    })
  }

  Update(type:string) {

    const modalwindow = this.modal.create({
      nzTitle: '功能信息编辑',
      nzContent: ZyzceditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        id: this.activatedNode.key,
        type: type,
        opreationtype:"update"
      },

      nzOnOk: () => new Promise(resolve => {
        modalwindow.getContentComponent().save();
        setTimeout(() => {
          resolve(true);
          this.getgnArrayNztree();
          this.getcdArrayNztree();
        }, 1000)
      })
    })
  }

  Add(type:string) {

    const modalwindow = this.modal.create({
      nzTitle: '功能信息编辑',
      nzContent: ZyzceditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        id: this.activatedNode.key,
        type: type,
        opreationtype:"add"
      },

      nzOnOk: () => new Promise(resolve => {
        modalwindow.getContentComponent().save();
        setTimeout(() => {
          resolve(true);
          this.getgnArrayNztree();
          this.getcdArrayNztree();
        }, 1000)
      })
    })
  }

  /* cdAdd(type:string) {
     const modalwindow = this.modal.create({
       nzTitle: '菜单信息编辑',
       nzContent: ZyzceditComponent,
       nzViewContainerRef: this.viewContainerRef,
       nzComponentParams: {
         id: this.activatedNode.key,
         type: type,
         opreationtype:"add"
       },

       nzOnOk: () => new Promise(resolve => {
         modalwindow.getContentComponent().save();
         setTimeout(() => {
           resolve(true);
           this.getcdArrayNztree()
         }, 1000)
       })
     })
   }
   cdUpdate(type:string) {
     const modalwindow = this.modal.create({
       nzTitle: '菜单信息编辑',
       nzContent: ZyzceditComponent,
       nzViewContainerRef: this.viewContainerRef,
       nzComponentParams: {
         id: this.activatedNode.key,
         type: type,
         opreationtype:"update"
       },

       nzOnOk: () => new Promise(resolve => {
         modalwindow.getContentComponent().save();
         setTimeout(() => {
           resolve(true);
           this.getcdArrayNztree()
         }, 1000)
       })
     })
   }
 */
  delete(){
    this.manageService.deleteRightresource(this.activatedNode.key).then((data:any)=>{
      if(data.msg == "ok"){
        this.alertService.showSuccess("删除成功")
        this.getcdArrayNztree()
        this.getgnArrayNztree()
      }else{
        this.alertService.showError("删除失败")
      }
    })
  }

}
